package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCahceAnnotation;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.dao.UserDao;
import com.baizhi.cmfz.entity.AddressDTO;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.service.UserService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class UserServiceImpl implements UserService {

	@Autowired(required = false)
	private UserDao userDao;
	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Map selectUserPage(int page, int limit, String name) {
//		处理参数
		if(name!=null && !"".equals(name)){
			name = "%"+name+"%";
		}else{
			name = null;
		}
		int offset = (page-1)*limit;
		List<User> users = userDao.selectUserPage(offset, limit, name);
		int count = userDao.selectCount(name);

		Map map = new HashMap();
		map.put("data",users);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;

	}

	@Override
	@Transactional
	@DeleteCahceAnnotation
	public Map userUpdate(int userId, int userStatus) {
		Map map = new HashMap();
		int i = userDao.userUpdate(userId,userStatus);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}
		return map;
	}


/*
	@Override
	public Map selectUser() {
		long start = new Date().getTime();
		Map map = new HashMap();
		List<Integer> userDTOS = userDao.selectUserDTO();
		List<Integer> sexDTOS = userDao.selectUserSexDTO();
		List<AddressDTO> addressDTOS = userDao.selectAddressCount();
		map.put("userDTO",userDTOS);
		map.put("sexDTO",sexDTOS);
		map.put("address",addressDTOS);
		long end = new Date().getTime();
		System.err.println(start-end);
		return map;
	}
*/

	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Map selectUser() {
		long start = new Date().getTime();
		Map  map = new HashMap<>();
//		创建一个数量为三的线程计数器
		CountDownLatch countDownLatch = new CountDownLatch(3);
//		创建第一个任务
		ThreadPollUtil.executor.execute(new Runnable() {
			@Override
			public void run() {
				List<Integer> userDTOS = userDao.selectUserDTO();
				map.put("userDTO",userDTOS);
//				计数器的数量值减一
				countDownLatch.countDown();
			}
		});

//		创建第二个任务
		ThreadPollUtil.executor.execute(new Runnable() {
			@Override
			public void run() {
				List<Integer> sexDTOS = userDao.selectUserSexDTO();
				map.put("sexDTO",sexDTOS);
//				计数器的值减一
				countDownLatch.countDown();
			}
		});

//		创建第三个任务

		ThreadPollUtil.executor.execute(new Runnable() {
			@Override
			public void run() {
				List<AddressDTO> addressDTOS = userDao.selectAddressCount();
				map.put("address",addressDTOS);
				countDownLatch.countDown();
			}
		});

		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long endTime = new Date().getTime();
		System.out.println(endTime-start);
//		long l = System.currentTimeMillis();

		return map;
	}
}
